<template>
	<tm-app ref="app">
		<tm-navbar title="订单评价" color="orange" linear="right" beforeBack="true" hideHome="true" @click="backPage"></tm-navbar>
		<tm-sheet>
			<tm-text :font-size="24" _class="font-weight-b" label="评分"></tm-text>
			<tm-divider></tm-divider>
			<tm-rate :color="['orange']" :defaultValue="star" @click="starEval" showLabel></tm-rate>
		</tm-sheet>
		<tm-sheet>
			<tm-text :font-size="30" _class="font-weight-b" label="评价内容" fontSize="19"></tm-text>
			<tm-divider></tm-divider>
			<tm-input type="textarea" focusColor="orange" autoHeight="true" height="220" v-model="content" placeholder="评价内容"></tm-input>
		</tm-sheet>
		<tm-button @click="saveEval()" style="margin-left: 2%;" color="orange" :width="700" label="保存"></tm-button>
	
		<tm-message ref="msg" :lines="5"></tm-message>
	
	</tm-app>
</template>


<script lang="ts" setup>
	import { ref, nextTick } from "vue"
	import { onLoad } from "@dcloudio/uni-app"
	import { isEmpty } from "@/tmui/tool/function/util";
	import tmMessage from "@/tmui/components/tm-message/tm-message.vue";
	import { saveOrderEval } from '@/api/order'
	
	const orderid = ref("");
	const userid = ref('');
	const content = ref("");
	const star = ref("5");
	const placement = ref('top');
	const msg = ref<InstanceType<typeof tmMessage> | null>(null)
	onLoad((e) => {
		// console.log(JSON.stringify(e));
		orderid.value = e.id;
		userid.value = uni.getStorageSync("userid");
	})
	
	function starEval(e : any){
		console.log(JSON.stringify(e));
	}
	
	
	function saveEval(){
		if (isEmpty(content.value)) {
			msg.value.show({ model: 'error', text: "评价内容不能为空！" })
			return false;
		}
		
		saveOrderEvalFun()
		
		
		return true;
	}
	
	const saveOrderEvalFun = async() => {
		let param = {"orderid":orderid.value,"wxid":userid.value,"content":content.value,"star":star.value};
		// console.log(JSON.stringify(param));
		const result = await saveOrderEval(param);
		if(result.status === "1"){
			msg.value.show({ model: 'success', text: "保存成功！" })
			setTimeout(function(){backOrderPage()},2000);
		}else{
			msg.value.show({ model: 'error', text: result.msg })
		}
	}
	
	function backOrderPage(){
		uni.redirectTo({
			url: '/pages/index/order?flag=false'
		})
	}
	
	function backPage(e : any){
		// console.log(JSON.stringify(e));
		uni.redirectTo({
			url: '/pages/index/order'
		})
	}
</script>